//
//  NSDictionary+log.m
//  XLProject
//
//  Created by xinglian on 2021/12/24.
//

#import "NSDictionary+log.h"
@implementation NSDictionary (log)
- (NSString *)description
{
    NSMutableString *strM = [NSMutableString stringWithString:@"\n{"];
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if ([obj isKindOfClass:[NSString class]])
        {
            [strM appendFormat:@" %@ = %@\n", key, obj];
        }
        else
        {
            [strM appendFormat:@" %@ = %@\n", key, [obj description]];
        }
    }];
    [strM appendString:@"}"];
    return strM;
}
- (NSString *)json
{
    NSData *data= [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
- (NSString *)spellWithURLEncode
{
    NSString *string = @"";
    for (NSString *key in [self allKeys]) {
        NSString *value = [NSString stringWithFormat:@"%@",[self valueForKey:key]];
        NSString *newString = [NSString stringWithFormat:@"%@=%@&",key, [self urlEncode:value]];
        string = [string stringByAppendingString:newString];
    }
    return [string substringToIndex:string.length-1];
}

- (NSString *)urlEncode:(NSString *)str
{
    if (!str) return nil;
    NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\|";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    return [str stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
}

@end
